using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;

namespace Uninstaller
{
    static class Program
    {
        private const string PRODUCT_CODE_FILE = "product-code.txt";

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length == 0)
                RemoveApp();

            try
            {
                switch (args[0])
                {
                    case "prepare":
                        PrepareUninstall(args[1]);
                        break;
                    case "unprepare":
                        UnprepareUninstall();
                        break;
                }
            }
            catch { }
            
            Application.Exit();
        }


        private static void PrepareUninstall(string productCode)
        {
            try
            {
                string filePath = Path.Combine(Application.StartupPath, PRODUCT_CODE_FILE);
                using (StreamWriter sw = new StreamWriter(filePath))
                {
                    sw.WriteLine(productCode);
                    sw.Flush();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(
                    "Exception preparing uninstall:\r\n\r\n[{0}]\r\n{1}",
                    ex.GetType(),
                    ex.Message),
                    "Log Analyzer Install",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }
        private static void UnprepareUninstall()
        {
            try
            {
                string filePath = Path.Combine(Application.StartupPath, PRODUCT_CODE_FILE);
                File.Delete(filePath);
            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(
                    "Exception unpreparing uninstall:\r\n\r\n[{0}]\r\n{1}",
                    ex.GetType(),
                    ex.Message),
                    "Log Analyzer Uninstall",
                    MessageBoxButtons.OK,
                    MessageBoxIcon.Error);
            }
        }

        private static void RemoveApp()
        {
            try
            {
                string filePath = Path.Combine(Application.StartupPath, PRODUCT_CODE_FILE);
                string productCode = null;
                using (StreamReader sr = new StreamReader(filePath))
                {
                    productCode = sr.ReadLine();
                }

                string msiexec = Path.Combine(
                    Environment.GetFolderPath(Environment.SpecialFolder.System),
                    "msiexec.exe");

                ProcessStartInfo psi = new ProcessStartInfo();
                psi.FileName = msiexec;
                psi.Arguments = "/x " + productCode;
                psi.CreateNoWindow = true;
                Process.Start(psi);

            }
            catch (Exception ex)
            {
                MessageBox.Show(string.Format(
                    "Exception uninstalling Log Analyzer:\r\n\r\n[{0}]\r\n{1}",
                    ex.GetType(),
                    ex.Message),
                    "Log Analyzer Uninstall",
                    MessageBoxButtons.OK, 
                    MessageBoxIcon.Error);
            }
        }

    }
}